package top.software.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import top.software.mapper.UserMapper;
import top.software.pojo.User;
import top.software.util.Message;
import top.software.util.MessageUtil;

/**
 * @ClassName: UserService
 * @Author: Amosen
 * @Since: 2021/4/20
 */

@Service
public class UserService {

	public static final Integer COMMON_USER = 0;

	public static final Integer ROOT = 1;

	public static final Integer MANAGER = 2;

	@Autowired
	private UserMapper userMapper = null;

	public Message login(String account, String password) {
		User user = userMapper.getUserByAccount(account);
		if (user == null) {
			return MessageUtil.fail();
		} else {
			String savedPassword = user.getPassword();
			if (savedPassword.equals(password)) {
				return MessageUtil.success().addData("user", user);
			} else {
				return MessageUtil.fail();
			}
		}
	}

	public Message register(String account, String password) {
		User savedUser = userMapper.getUserByAccount(account);
		if (savedUser != null) {
			return MessageUtil.accountExists();
		} else {
			User rawUser = new User(account, password, COMMON_USER);
			userMapper.addUser(rawUser);
			return MessageUtil.success();
		}
	}

	public Message updatePassword(String account, String password) {
		User savedUser = userMapper.getUserByAccount(account);
		if (savedUser == null) {
			return MessageUtil.notExists();
		} else {
			int savedAuth = savedUser.getAuth();
			User rawUser = new User(account, password, savedAuth);
			userMapper.updateUser(rawUser);
			return MessageUtil.success();
		}
	}

	// 权限校验，是否可以使用管理员权限
	private boolean canUseManage(String account) {
		if (account == null) {
			return false;
		}
		User savedUser = userMapper.getUserByAccount(account);
		if (savedUser == null) {
			return false;
		} else {
			int savedAuth = savedUser.getAuth();
			return savedAuth == MANAGER || savedAuth == ROOT;
		}
	}

	// 权限校验，是否可以使用root权限
	private boolean canUseRoot(String account) {
		if (account == null) {
			return false;
		}
		User savedUser = userMapper.getUserByAccount(account);
		if (savedUser == null) {
			return false;
		} else {
			int auth = savedUser.getAuth();
			return auth == ROOT;
		}
	}

	public Message modifyQualification(String loginAccount, String modifyAccount, Integer newAuth) {
		if (!canUseManage(loginAccount)) {
			return MessageUtil.lowAuth();
		} else {
			User savedUser = userMapper.getUserByAccount(modifyAccount);
			if (savedUser == null) {
				return MessageUtil.notExists();
			} else {
				if (newAuth.intValue() != ROOT.intValue() && newAuth.intValue() != MANAGER.intValue()
						&& newAuth.intValue() != COMMON_USER.intValue()) {
					return MessageUtil.fail();
				}
				String savedPassword = savedUser.getPassword();
				User rawUser = new User(modifyAccount, savedPassword, newAuth);
				userMapper.updateUser(rawUser);
				return MessageUtil.success();
			}
		}
	}

	public Message getAllUser() {
		List<User> allUser = userMapper.getAllUser();
		for (User user : allUser) {
			user.setPassword("");
		}
		return MessageUtil.success().addData("user", allUser);
	}

	public Message getUserVague(String account, String username) {
		List<User> users;
		if (account != null) {
			users = userMapper.getUserVague(account, null);
		} else if (username != null) {
			users = userMapper.getUserVague(null, username);
		} else {
			users = userMapper.getUserVague(null, null);
		}
		for (User user : users) {
			user.setPassword("");
		}
		return MessageUtil.success().addData("user", users);
	}

	public Message screenQualification(Integer auth) {
		List<User> userByAuth = userMapper.getUserByAuth(auth);
		return MessageUtil.success().addData("user", userByAuth);
	}

	public Message addUser(String loginAccount, String account, Integer auth) {
		if (!canUseManage(loginAccount)) {
			return MessageUtil.lowAuth();
		} else {
			if ((!canUseRoot(loginAccount)) && auth.intValue() == ROOT) {
				return MessageUtil.lowAuth();
			}
			User savedUser = userMapper.getUserByAccount(account);
			if (savedUser != null) {
				return MessageUtil.accountExists();
			} else {
				userMapper.addUser(new User(account, account, auth));
				return MessageUtil.success();
			}
		}
	}

	public Message deleteUser(String loginAccount, String account) {
		if (!canUseManage(loginAccount)) {
			return MessageUtil.lowAuth();
		} else {
			User savedUser = userMapper.getUserByAccount(account);
			if (savedUser == null) {
				return MessageUtil.notExists();
			} else {
				int savedAuth = savedUser.getAuth();
				if (savedAuth == ROOT && (!canUseRoot(loginAccount))) {
					return MessageUtil.notExists();
				} else {
					userMapper.deleteUser(account);
					return MessageUtil.success();
				}
			}
		}
	}
}
